<?php

namespace App\Console\Commands;

use App\Models\AuctionModel;
use App\Models\OpenIdModel;
use App\Models\ShopUserModel;
use Exception;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Model;

class OneKeyPushCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:oneKeyPush {serialNumber}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     *
     * @return void
     * @throws Exception
     *
     */
    public function handle()
    {
        $serialNumber = $this->argument('serialNumber');
        $model = AuctionModel::findBySerialNumber($serialNumber);

        $twoPush = $model->two_push;
        usort($twoPush, function ($a, $b) {
            if ($a['timer'] == $b['timer']) {
                return 0;
            }
            return ($a['timer'] < $b['timer']) ? 1 : -1;
        });

        foreach ($twoPush as $value) {
            $diffSeconds = now()->diffInSeconds(now()->parse($model->start_at)->subMinute($value['timer']));//距今还有多少秒
            $smsData = '';
            if ((int)$value['crowd'] === 1) {
                if (count($value['method']) === 0) return;
                else if (count($value['method']) === 2) {//微信和短信同时推送
//                    $smsData = $model->id . '-' . $value['crowd'] . '-4-' . $value['timer'] . '-' . $value['method'][0];
                    $smsData = $model->id . '-' . $value['crowd'] . '-4-3-' . $value['timer'];
                    dd($smsData);
//                    $wechatData = self::suitWechatData($value, $model->id);
                } else if ((int)$value['method'][0] === 1) return;//$wechatData = self::suitWechatData($value);
//                else
//                    $smsData = self::suitSmsData($value, $model);
            }

            if ($smsData !== '') pushSmsMessage($smsData);
        }
    }

    private function suitSmsData(array $data, Model $model): array
    {
        $result = [];
        if ((int)$data['crowd'] === 1) {
            $result['content']['user_info'] = ShopUserModel::query()->where(['user_status' => 1])
//                ->whereIn('user_id', [1,2,3,4,5,6,7,8])
                ->get(['user_mobile', 'user_id'])->toArray();
        } else {
            $result['content']['user_info'] = ShopUserModel::query()
                ->leftJoin('auction_follow', 'auction_follow.user_id', 'shop_user.user_id')
                ->where('auction_follow.auction_id', $model->id)
                ->where(['user_status' => 1, 'is_subscribe' => 1])->get(['user_mobile', 'user_id'])->toArray();
        }

        $result['id'] = $model->id;
        $result['crowd'] = (int)$data['crowd'];
        $result['type'] = 4;
        $result['ext'] = [
            'num' => $data['timer'],
        ];
        return $result;
    }

//    private function suitWechatData($data)
//    {
//        if((int)$data['crowd'] === 1) {
//            $mobile = OpenIdModel::query()->where([
//                'plateform' => 2,
//                'type' => 'wx'
//            ])->value('openid');
//        }
//        else {
//            $mobile = ShopUserModel::query()
//                ->leftJoin('auction_follow', 'auction_follow.user_id', 'shop_user.user_id')
//                ->where('auction_follow.user_id', $id)
//                ->where(['user_status' => 1, 'is_subscribe' => 1])->pluck('user_mobile');
//        }
//    }
}
